package FifthQuestion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author cyt
 * @date 2020/6/14
 */
public class CardGame {
    //扑克牌
    private List<Card> cards;
    //三位玩家
    private Player p1;

    private Player p2;

    private Player p3;

    //初始化玩家
    public CardGame(String p1Name, String p2Name, String p3Name){
        p1 = new Player(p1Name);
        p2 = new Player(p2Name);
        p3 = new Player(p3Name);
        this.cards = new ArrayList<>();
    }



    /**
     * 初始化54张牌
     */
    public void initCards(){
        Card card;
        //初始化除了大王小王的牌
        for(int i = 0; i <= CardType.values().length - 1; i++){
            for(int j = 2; j <= CardValue.values().length - 1; j++){
                card = new Card(CardValue.values()[j],CardType.values()[i]);
                this.cards.add(card);
            }
        }
        //初始化大王和小王
        cards.add(new Card(CardValue.values()[0],null));
        cards.add((new Card(CardValue.values()[1],null)));


    }

    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(this.cards);
    }

    //开始游戏，依次摸牌
    public void startGame(){
        System.out.println("欢迎来到终极地狱斗地主，参与的勇士有：");
        System.out.println(p1.getName() +", "+p2.getName()+", "+p3.getName());
        System.out.println("开始发牌");
        int index = 0;
        //依次摸牌，剩三张
        while (index <= 50){
            p1.addCard(this.cards.get(index++));
            p2.addCard(this.cards.get(index++));
            p3.addCard(this.cards.get(index++));
        }
    }

    /**
     * 将手牌从大到小排序
     * @param cards
     *
     */
    public void sort(List<Card> cards){
        Collections.sort(cards,(Card o1, Card o2) ->{
            return o2.getCardValue().getValue() - o1.getCardValue().getValue();
        });
    }
    //展示三个玩家的手牌和底牌
    public void showAll(){

        System.out.println(p1.getName()+"玩家的手牌为：");
        sort(p1.getCardList());
        System.out.println(p1.getCardList());
        System.out.println(p2.getName()+"玩家的手牌为：");
        sort(p2.getCardList());
        System.out.println(p2.getCardList());
        System.out.println(p3.getName()+"玩家的手牌为：");
        sort(p3.getCardList());
        System.out.println(p3.getCardList());
        System.out.println("底牌为：");
        for(int i = 51; i <= 53; i++){
            System.out.print(this.cards.get(i)+"  ");
        }


    }
}
